<?php


namespace app\api\controller\v35;


use app\api\controller\v2\Auth;
use app\common\cachemodel\zl2\Zl2LuckyPartnerRids;
use app\common\lib\astrolog\Astrolog;
use app\common\lib\astrolog\DailyFortune;
use app\common\lib\astrolog\LogicBase;

class FortuneLuckyPartner extends Auth
{
    /**
     * 获取幸运结缘结果
     */
    public function getLuckyPartner()
    {
        $luckyPartnerRidsModel = new Zl2LuckyPartnerRids();

        $ridData = $luckyPartnerRidsModel->getLuckyPartnerRids($this->user['id']);
        $resList = [];
        if ($ridData)
        {
            $rids = json_decode($ridData['rids'], true);
            $recodeModel = new \app\common\cachemodel\Recode();
            $recodeList = [];
            $ridsList = [];
            foreach ($rids as $ridItem)
            {
                $recode = $recodeModel->getInfoById($ridItem);
                if (!$recode || $recode['uid'] != $this->user['id'] || $recode['status'] != 1 || (int)$recode['isself'] === 1)
                {
                    continue;
                }
                $recodeList[$ridItem] = $recode;
                $ridsList[] = $ridItem;
            }
            // 有结缘 档案 则计算结缘结果
            if ($ridsList)
            {
                $selfRecord = $recodeModel->getInfoById($this->user['rid']);
                $ridsList[] = $this->user['id'];
                $hash = date('Ymd') . ':' . md5(json_encode($ridsList));

                $data = $luckyPartnerRidsModel->getLuckyPartner($this->user['id'], $hash);

                if (!$data)
                {
                    $data = DailyFortune::getInstance()
                        ->setAdvance(date('Y-m-d'))
                        ->setMainRecord($selfRecord)
                        ->setOtherRecordList($recodeList)
                        ->luckyPartner();

                    $luckyPartnerRidsModel->setLuckyPartner($this->user['id'], $hash, $data);
                }
                /**
                 * 今日最佳的互动星缘: 第一名且分数为正
                 * 或许不错的无奈之选: 第一名且分数不为正
                 * 非第一名，且双方合盘满足一定条件: 幸运长随的完美拍档
                 *      双方月亮都与对方日月金木之一成非刑冲相或合轴
                 *      双方金星都与对方日月金木之一成非刑冲相或合轴
                 * 非第一名，分数为负: 玄机莫测的神秘黑洞
                 * 非第一名，分数0-50: 闪耀温暖的微弱烛光
                 * 非第一名，50-100: 温馨美好的夏日清风
                 * 非第一名，100-150: 丰沛滋润的快乐源泉
                 * 非第一名，分数150以上: 播撒幸运的人形太阳
                 * 最后一名，且得分为负: 隐有风雷的未知按键
                 */
                foreach ($data as $k => $datum)
                {
                    $datum['name'] = $recodeList[$datum['rid']]['name'] ?? '';
                    $datum['avatar'] = $recodeList[$datum['rid']]['avatar'] ?? '';
//                    $datum['tips'] = $tips[$k] ?? '幸运长随的完美拍档';
                    $datum['ranking'] = $k + 1;
                    $datum['score'] = (int)$datum['score'];
                    $datum['tips'] = '玄机莫测的神秘黑洞';
                    // 第一名
                    if ($datum['score'] >= $data[0]['score'])
                    {
                        if ($datum['score'] > 0)
                        {
                            $datum['tips'] = '今日最佳的互动星缘';
                        } else
                        {
                            $datum['tips'] = '或许不错的无奈之选';
                        }
                    } // 最后一名，且得分为负
                    elseif ($datum['score'] < 0 && $k === count($data) - 1)
                    {
                        $datum['tips'] = '隐有风雷的未知按键';
                    } else
                    {
                        // 判断双方合盘是否满足条件
                        $isOk = false;
                        $synastryData = Astrolog::getInstance()
                            ->setExecPrefix(config('astrolog.prefix'))
                            ->setUserSetting(config('astroset.setting1'))
                            ->setRecode1($selfRecord)
                            ->setRecode2($recodeList[$datum['rid']])
                            ->setGetDatas(['synastry'])
                            ->getAstroDatas()
                            ->getDatas('synastry');
                        $synastryData = json_decode(json_encode($synastryData), true);
                        /**
                         * 同时满足下方两个条件
                         * 双方月亮都与对方日月金木之一成非刑冲相或合轴
                         * 双方金星都与对方日月金木之一成非刑冲相或合轴
                         */
                        if (!empty($synastryData[0]['planets'][1]['phase'])
                            && !empty($synastryData[0]['planets'][3]['phase'])
                            && !empty($synastryData[1]['planets'][1]['phase'])
                            && !empty($synastryData[1]['planets'][3]['phase'])
                        )
                        {
                            if ((((isset($synastryData[0]['planets'][1]['phase']['1-0'])
                                            && in_array($synastryData[0]['planets'][1]['phase']['1-0']['phase_e'], [0, 60, 120], false))
                                        || (isset($synastryData[0]['planets'][1]['phase']['1-1'])
                                            && in_array($synastryData[0]['planets'][1]['phase']['1-1']['phase_e'], [0, 60, 120], false))
                                        || (isset($synastryData[0]['planets'][1]['phase']['1-3'])
                                            && in_array($synastryData[0]['planets'][1]['phase']['1-3']['phase_e'], [0, 60, 120], false))
                                        || (isset($synastryData[0]['planets'][1]['phase']['1-5'])
                                            && in_array($synastryData[0]['planets'][1]['phase']['1-5']['phase_e'], [0, 60, 120], false))
                                        || (isset($synastryData[0]['planets'][1]['phase']['1-17'])
                                            && in_array($synastryData[0]['planets'][1]['phase']['1-17']['phase_e'], [90, 180], false))
                                        || (isset($synastryData[0]['planets'][1]['phase']['1-20'])
                                            && in_array($synastryData[0]['planets'][1]['phase']['1-20']['phase_e'], [90, 180], false)))
                                    && ((isset($synastryData[0]['planets'][3]['phase']['3-0'])
                                            && in_array($synastryData[0]['planets'][3]['phase']['3-0']['phase_e'], [0, 60, 120], false))
                                        || (isset($synastryData[0]['planets'][3]['phase']['3-1'])
                                            && in_array($synastryData[0]['planets'][3]['phase']['3-1']['phase_e'], [0, 60, 120], false))
                                        || (isset($synastryData[0]['planets'][3]['phase']['3-3'])
                                            && in_array($synastryData[0]['planets'][3]['phase']['3-3']['phase_e'], [0, 60, 120], false))
                                        || (isset($synastryData[0]['planets'][3]['phase']['3-5'])
                                            && in_array($synastryData[0]['planets'][3]['phase']['3-5']['phase_e'], [0, 60, 120], false))
                                        || (isset($synastryData[0]['planets'][3]['phase']['3-17'])
                                            && in_array($synastryData[0]['planets'][3]['phase']['3-17']['phase_e'], [90, 180], false))
                                        || (isset($synastryData[0]['planets'][3]['phase']['3-20'])
                                            && in_array($synastryData[0]['planets'][3]['phase']['3-20']['phase_e'], [90, 180], false))))
                                && (((isset($synastryData[1]['planets'][1]['phase']['1-0'])
                                            && in_array($synastryData[1]['planets'][1]['phase']['1-0']['phase_e'], [0, 60, 120], false))
                                        || (isset($synastryData[1]['planets'][1]['phase']['1-1'])
                                            && in_array($synastryData[1]['planets'][1]['phase']['1-1']['phase_e'], [0, 60, 120], false))
                                        || (isset($synastryData[1]['planets'][1]['phase']['1-3'])
                                            && in_array($synastryData[1]['planets'][1]['phase']['1-3']['phase_e'], [0, 60, 120], false))
                                        || (isset($synastryData[1]['planets'][1]['phase']['1-5'])
                                            && in_array($synastryData[1]['planets'][1]['phase']['1-5']['phase_e'], [0, 60, 120], false))
                                        || (isset($synastryData[1]['planets'][1]['phase']['1-17'])
                                            && in_array($synastryData[1]['planets'][1]['phase']['1-17']['phase_e'], [90, 180], false))
                                        || (isset($synastryData[1]['planets'][1]['phase']['1-20'])
                                            && in_array($synastryData[1]['planets'][1]['phase']['1-20']['phase_e'], [90, 180], false)))
                                    && ((isset($synastryData[1]['planets'][3]['phase']['3-0'])
                                            && in_array($synastryData[1]['planets'][3]['phase']['3-0']['phase_e'], [0, 60, 120], false))
                                        || (isset($synastryData[1]['planets'][3]['phase']['3-1'])
                                            && in_array($synastryData[1]['planets'][3]['phase']['3-1']['phase_e'], [0, 60, 120], false))
                                        || (isset($synastryData[1]['planets'][3]['phase']['3-3'])
                                            && in_array($synastryData[1]['planets'][3]['phase']['3-3']['phase_e'], [0, 60, 120], false))
                                        || (isset($synastryData[1]['planets'][3]['phase']['3-5'])
                                            && in_array($synastryData[1]['planets'][3]['phase']['3-5']['phase_e'], [0, 60, 120], false))
                                        || (isset($synastryData[1]['planets'][3]['phase']['3-17'])
                                            && in_array($synastryData[1]['planets'][3]['phase']['3-17']['phase_e'], [90, 180], false))
                                        || (isset($synastryData[1]['planets'][3]['phase']['3-20'])
                                            && in_array($synastryData[1]['planets'][3]['phase']['3-20']['phase_e'], [90, 180], false)))))
                            {
                                $isOk = true;
                            }
                        }
                        if ($isOk)
                        {
                            $datum['tips'] = '幸运长随的完美拍档';
                        } elseif ($datum['score'] >= 150)
                        {
                            $datum['tips'] = '播撒幸运的人形太阳';
                        } elseif ($datum['score'] >= 100)
                        {
                            $datum['tips'] = '丰沛滋润的快乐源泉';
                        } elseif ($datum['score'] >= 50)
                        {
                            $datum['tips'] = '温馨美好的夏日清风';
                        } elseif ($datum['score'] >= 0)
                        {
                            $datum['tips'] = '闪耀温暖的微弱烛光';
                        } else
                        {
                            $datum['tips'] = '玄机莫测的神秘黑洞';
                        }
                    }
                    $datum['tips'] = 'TA是你“' . $datum['tips'] . '”';
                    $data[$k] = $datum;
                }
                $resList = $data;
            }
        }

        $resData = [
            'title' => date('m月d日') . '结缘排行榜',
            'list' => $resList
        ];

        return show(config('code.success'), '请求成功', $resData);
    }

    public function updateRids()
    {
        $ridsStr = input('rids/s', '');

        $rids = [];
        if ($ridsStr)
        {
            $rids = explode(',', $ridsStr);

            $recodeModel = new \app\common\cachemodel\Recode();
            foreach ($rids as $ridItem)
            {
                $recode = $recodeModel->getInfoById($ridItem);
                if (!$recode || $recode['uid'] != $this->user['id'] || $recode['status'] != 1)
                {
                    return show(config('code.error'), '选择的档案不存在');
                }

                if ((int)$recode['isself'] === 1)
                {
                    return show(config('code.error'), '不能选择自己的档案');
                }
            }
        }

        $luckyPartnerRidsModel = new Zl2LuckyPartnerRids();

        $res = $luckyPartnerRidsModel->UpdateData($this->user['id'], json_encode($rids, JSON_UNESCAPED_UNICODE));

        if ($res)
        {
            return show(config('code.success'), '更新成功');
        } else
        {
            return show(config('code.error'), '操作异常，请稍后再试~');
        }
    }

}
